package com.swsc.xapp.model;

import com.swsc.xapp.utils.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author chencl
 * @Date 2022/1/18 15:04
 * @Version 1.0
 * @Description PageInfo
 */
public class PageInfo<T> {
    protected List<T> result;
    protected long totalCount;
    protected int pageSize;
    protected int pageNo;
    protected int pageCount;

    protected String remark;

    public PageInfo(List<T> result, long totalCount, int pageNo, int pageSize) {
        this.result = result;
        this.totalCount = totalCount;
        this.pageNo = pageNo;
        this.pageSize = pageSize;

        int pageCount = 0;
        if(totalCount > 0){
            pageCount = (int) (totalCount / pageSize);
            int mod = (int) (totalCount % pageSize);
            if(mod > 0){
                pageCount += 1;
            }
        }
        this.pageCount = pageCount;
    }

    public PageInfo(int pageNo, int pageSize) {
        this(new ArrayList<>(),0,pageNo,pageSize);
    }

    public PageInfo() {
        this.result = new ArrayList<>();
        this.totalCount = 0L;
        this.pageNo = 1;
        this.pageSize = 10;
        this.pageCount = 0;
    }

    public <R> PageInfo<R> mapPage(Function<T, R> mapper) {
        PageInfo<R> rPageInfo = new PageInfo<>();
        rPageInfo.setPageNo(pageNo);
        rPageInfo.setPageSize(pageSize);
        rPageInfo.setTotalCount(totalCount);
        rPageInfo.setPageCount(pageCount);
        if (Objects.nonNull(result)) {
            rPageInfo.setResult(result.stream().map(mapper).collect(Collectors.toList()));
        }
        return rPageInfo;
    }

    public <R> PageInfo<R> mapPage(Class<R> tClass) {
        PageInfo<R> rPageInfo = new PageInfo<>();
        rPageInfo.setPageNo(pageNo);
        rPageInfo.setPageSize(pageSize);
        rPageInfo.setTotalCount(totalCount);
        rPageInfo.setPageCount(pageCount);
        if (Objects.nonNull(result)) {
            rPageInfo.setResult(BeanUtils.copyListProperties(result, tClass));
        }
        return rPageInfo;
    }

    /**
     * 分页记录列表
     *
     * @return 分页对象记录列表
     */
    public List<T> getResult() {
        return this.result;
    }

    /**
     * 设置分页记录列表
     *
     * @param result
     */
    public PageInfo<T> setResult(List<T> result) {
        this.result = result;
        return this;
    }

    /**
     * 当前满足条件总行数
     *
     * @return 总条数
     */

    public long getTotalCount() {
        return this.totalCount;
    }

    /**
     * 设置当前满足条件总行数
     *
     * @param totalCount
     */
    public PageInfo<T> setTotalCount(long totalCount) {
        this.totalCount = totalCount;
        return this;
    }

    /**
     * 获取每页显示条数
     *
     * @return 每页显示条数
     */
    public int getPageSize() {
        return this.pageSize;
    }

    /**
     * 设置每页显示条数
     *
     * @param pageSize
     */
    public PageInfo<T> setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    /**
     * 当前页
     *
     * @return 当前页
     */
    public long getPageNo() {
        return this.pageNo;
    }

    /**
     * 设置当前页
     *
     * @param pageNo
     */
    public PageInfo<T> setPageNo(int pageNo) {
        this.pageNo = pageNo;
        return this;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public void pageCount(){
        int pgcnt = (int) ((totalCount / pageSize) + (totalCount % pageSize > 0 ? 1 : 0));
        this.pageCount = pgcnt;
    }
}
